package cn.gz.demo.utils;

import android.text.TextUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by leo on 16/3/4.
 */
public class DateUtils {
    private static Calendar mCalendar;

    public static Calendar getCalendar() {
        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
        }
        return mCalendar;
    }

    // 获取昨天时间的最小值：
    public static long getYesterdayMinTimeMillis(long mHourInMillis) {
        long currTime = System.currentTimeMillis();
        getCalendar().setTime(new Date(currTime));
        int year = getCalendar().get(Calendar.YEAR);
        int month = getCalendar().get(Calendar.MONTH);
        int day = getCalendar().get(Calendar.DAY_OF_MONTH);
        getCalendar().set(year, month, day, 0, 0, 0);
        long minToday = getCalendar().getTimeInMillis() - mHourInMillis;
        return minToday;
    }

    //  获取昨天时间的最大值：
    public static long getYesterdayMaxTimeMillis(long mHourInMillis) {
        long currTime = System.currentTimeMillis();
        getCalendar().setTime(new Date(currTime));

        int year = getCalendar().get(Calendar.YEAR);
        int month = getCalendar().get(Calendar.MONTH);
        int day = getCalendar().get(Calendar.DAY_OF_MONTH);
        getCalendar().set(year, month, day, 23, 59, 59);
        long minToday = mCalendar.getTimeInMillis() - mHourInMillis;

        return minToday;
    }


    /**
     * 获取当前时间毫秒值
     *
     * @param dateStr
     * @return
     */
    public static long getTimeMillis(String dateStr) {

        try {
            Date d = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            d = sf.parse(dateStr);
            return d.getTime();
        } catch (Exception e) {

        }
        return 0;
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isToday(String day) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = getDateFormat().parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            cal.setTime(date);
            if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
                int diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR);
                if (diffDay == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    /**
     * 返回yyyy-MM-dd格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String getDate(long date) {
        if (date <= 0) {
            return "";
        }
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ctime = formatter.format(new Date(date));
        return ctime;
    }

    public static String getDateTime(long date) {
        if (date <= 0) {
            return "";
        }
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("MM-dd HH:mm:ss");
        String ctime = formatter.format(new Date(date));
        return ctime;
    }

    public static String getDate2(long date) {
        if (date <= 0) {
            return "";
        }
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("HH:mm:ss");
        String ctime = formatter.format(new Date(date));
        return ctime;
    }

    public static String getDate(Date date, String format) {
        if (date == null || TextUtils.isEmpty(format))
            return "";
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(format);
        String ctime = formatter.format(date);
        return ctime;
    }

    public static String getTime(long date, String format) {
        if (date <= 0 || TextUtils.isEmpty(format))
            return "";
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(format);
        String ctime = formatter.format(new Date(date));
        return ctime;
    }

    /**
     * 返回yyyy-MM-dd格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ctime = formatter.format(date);
        return ctime;
    }

    /**
     * 获取当前时间yyyy-MM-dd
     */
    public static String getNowDate(long datatime) {
        Date date = new Date(datatime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 格式化Date时间
     *
     * @param time       Date类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat) {
        DateFormat dateFormat = new SimpleDateFormat(timeFromat);
        return dateFormat.format(time);
    }

    /**
     * 格式化Timestamp时间
     *
     * @param timestamp  Timestamp类型时间
     * @param timeFromat
     * @return 格式化后的字符串
     */
    public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
        SimpleDateFormat df = new SimpleDateFormat(timeFromat);
        return df.format(timestamp);
    }

    /**
     * 订单倒计时获取时间
     *
     * @param nowTime
     * @param startTime
     * @return
     */
    public static String differenceTime(long nowTime, long startTime) {
        String msg = "";
        if (nowTime > 0 && nowTime > startTime) {
            long counttime = nowTime - startTime;

            String hoursMsg = "";
            String minutesMsg = "";
            String secondMsg = "";

            long hours = counttime / (1000 * 60 * 60);
            long minutes = (counttime - hours * (1000 * 60 * 60)) / (1000 * 60);
            long second = (counttime - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;

            if (hours == 0) {
                hoursMsg = "00";
            } else if (hours < 10) {
                hoursMsg = "0" + hours;
            } else {
                hoursMsg = "" + hours;
            }

            if (minutes == 0) {
                minutesMsg = "00";
            } else if (minutes < 10) {
                minutesMsg = "0" + minutes;
            } else {
                minutesMsg = "" + minutes;
            }

            if (second == 0) {
                secondMsg = "00";
            } else if (second < 10) {
                secondMsg = "0" + second;
            } else {
                secondMsg = "" + second;
            }

            msg = hoursMsg + ":" + minutesMsg + ":" + secondMsg;
        } else {
            msg = "";
        }
        return msg;
    }

    /**
     * 判断是不是今天
     *
     * @param date
     * @return
     */
    public static boolean checkIsToday(String date) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date history;
        try {
            history = formater.parse(date);
        } catch (ParseException e) {
            return false; // 格式不对，直接返回原来数据
        }
        Date current = new Date();
        // 判断年月
        if (current.getYear() == history.getYear() && current.getMonth() == history.getMonth() && current.getDate() == history.getDate()) {
            return true;
        }
        return false;
    }

    /**
     * 消息中心时间处理
     *
     * @param date
     * @return
     */
    public static String getMsgYear(long date) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("MM-dd");
        String ctime = formatter.format(date);
        return ctime;
    }

    public static String getMsgDate(long date) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("HH:mm");
        String ctime = formatter.format(date);
        return ctime;
    }

    public static String getMsgDate_1(long date) {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String ctime = formatter.format(date);
        return ctime;
    }

    public static boolean isSameWeekWithToday(Date date) {

        if (date == null)
            return false;
        Calendar todayCal = Calendar.getInstance();
        Calendar dateCal = Calendar.getInstance();
        todayCal.setTime(new Date());
        dateCal.setTime(date);
        // 1.比较当前日期在年份中的周数是否相同
        if (todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 消息中心列表时间处理
     *
     * @param time
     * @return
     */
    public static String msgDataDispose(long time) {
        if (time <= 0) {
            return "";
        }
        long nowTime = System.currentTimeMillis();
        if (time > nowTime) {
            return getDate(time);
        } else {
            Date nowDate = new Date();
            Date hisDate = new Date(time);
            if ((nowDate.getDate() - hisDate.getDate()) > 0) {
                // 判断是否是同一周
                if (isSameWeekWithToday(hisDate)) {
                    return getWeek(hisDate);
                } else {
                    return getNowDate(time);
                }
            } else {
                return getMsgDate(time);
            }
        }
    }

    /*获取星期几*/
    public static String getWeek(Date date) {
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    /**
     * 判断是不是同一个月
     *
     * @param time
     * @return
     */
    public static boolean isSameYearFormateDate(long time) {
        boolean isYear = false;
        Date history = new Date(time);
        Date current = new Date();
        // 判断年月
        if (current.getYear() == history.getYear() && current.getMonth() == history.getMonth()) {
            isYear = true;
        }
        return isYear;
    }

    /*
 * 毫秒转化时分秒毫秒
 */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day >= 0) {
            sb.append(day + "天");
        }
        if (hour >= 0) {
            sb.append(hour + "小时");
        }
        if (minute >= 0) {
            sb.append(minute + "分");
        }
        if (second >= 0) {
            sb.append(second + "秒");
        }
//        if (milliSecond >= 0) {
//            sb.append(milliSecond + "毫秒");
//        }
        return sb.toString();
    }

    public static String formatTakeTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append((day >= 10 ? day + "" : "0" + day) + ":");
        }
        if (hour >= 0) {
            sb.append((hour >= 10 ? hour + "" : "0" + hour) + ":");
        }
        if (minute >= 0) {
            sb.append((minute >= 10 ? minute + "" : "0" + minute) + ":");
        }
        if (second >= 0) {
            sb.append((second >= 10 ? second + "" : "0" + second) + ":");
        }
//        if (milliSecond >= 0) {
//            sb.append(milliSecond + "毫秒");
//        }
        String time = sb.toString();
        time = time.endsWith(":") ? time.substring(0, time.length() - 1) : time;
        return time;
    }

    /**
     * 判断是否是同一周
     *
     * @param date
     * @return
     */
    public static boolean isTheSameWeek(Date date) {
        Boolean isThisWeek = true;
        if (date == null) {
            isThisWeek = false;
        }
        // 先把Date类型的对象转换Calendar类型的对象
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(new Date());
        cal2.setTime(date);
        // 比较当前日期在年份中的周数是否相同
        if ((cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) && (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))) {
            isThisWeek = true;
        } else {
            isThisWeek = false;
        }
        return isThisWeek;
    }
}
